package com.sunjs.wechat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sunjs.model.LyUseraddr;
import com.sunjs.model.back.RestCode;
import com.sunjs.model.back.RestResponse;
import com.sunjs.wechat.service.WxUseraddrService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/wx/Useraddr")
public class WxUseraddrController {
	
	@Autowired
	private WxUseraddrService  wxUseraddrService;
	
	/**
	 * 根据用户id查询地址列表
	 * @param lyUseraddr
	 * @return
	 */
	@RequestMapping(value = "/selectUseraddrs", method = RequestMethod.POST)
	public RestResponse<List<LyUseraddr>> select(@RequestBody LyUseraddr lyUseraddr) {
		try {
			List<LyUseraddr> lyUseraddrList = wxUseraddrService.selectUseradderList(lyUseraddr);
			return  RestResponse.success(lyUseraddrList);
		} catch (Exception e) {
			return RestResponse.error(RestCode.FAIL);
		}
	}
	
	/**
	 * 根据用户ID查询默认地址
	 * @param lyUseraddr
	 * @return
	 */
	@RequestMapping(value = "/selectDefault", method = RequestMethod.POST)
    public RestResponse<LyUseraddr> selectDefault(@RequestBody LyUseraddr lyUseraddr) {
        try {
            LyUseraddr useraddr = wxUseraddrService.selectDefault(lyUseraddr);
            return  RestResponse.success(useraddr);
        } catch (Exception e) {
            return RestResponse.error(RestCode.FAIL);
        }
    }
	
	
	/**
	 * 新增收货地址
	 * @param lyUseraddr
	 * @return
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
    public RestResponse<Map<String, Object>> insert(@RequestBody LyUseraddr lyUseraddr) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObject = wxUseraddrService.insert(lyUseraddr);
            String code = (String) jsonObject.get("code");
            String msg = (String)jsonObject.get("msg");
            map.put("code", code);
            map.put("msg", msg);
            if(code.equals("1")){
                return RestResponse.checkError(1,msg);
            }
            return RestResponse.success(map);
        } catch (Exception e) {
            return RestResponse.error(RestCode.FAIL);
        }
    }
	
	/**
	 * 编辑收货地址
	 * @param lyUseraddr
	 * @return
	 */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RestResponse<Map<String, Object>> update(@RequestBody LyUseraddr lyUseraddr) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObject = wxUseraddrService.updateUseraddr(lyUseraddr);
            String code = (String) jsonObject.get("code");
            String msg = (String)jsonObject.get("msg");
            map.put("code", code);
            map.put("msg", msg);
            if(code.equals("1")){
                return RestResponse.checkError(1,msg);
            }
            return RestResponse.success(map);
        } catch (Exception e) {
            return RestResponse.error(RestCode.FAIL);
        }
    }
	
    
    /**
     * 删除收货地址
     * @param lyUseraddr
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public RestResponse<Map<String, Object>> delete(@RequestBody LyUseraddr lyUseraddr) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObject = wxUseraddrService.deleteUseraddr(lyUseraddr);
            String code = (String) jsonObject.get("code");
            String msg = (String)jsonObject.get("msg");
            map.put("code", code);
            map.put("msg", msg);
            if(code.equals("1")){
                return RestResponse.checkError(1,msg);
            }
            return RestResponse.success(map);
        } catch (Exception e) {
            return RestResponse.error(RestCode.FAIL);
        }
    }
	
}


